﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;

namespace YJKColumnModify.ConnectColumnBream
{
	internal class BreamConnectColumnGeometryToos
	{
		public static double Millimeter_To_Feet
		{
			get
			{
				return BreamConnectColumnGeometryToos.dMILLIMETER_TO_FEET;
			}
		}

		public static double OneMillimeter
		{
			get
			{
				return 1.0 / BreamConnectColumnGeometryToos.dMILLIMETER_TO_FEET;
			}
		}

		public static Solid GetFamliySymbolSolid(FamilySymbol symbol, ViewDetailLevel standard)
		{
			GeometryElement gElement = symbol.get_Geometry(new Options
			{
				ComputeReferences = true,
				DetailLevel = standard
			});
			Solid result = null;
			BreamConnectColumnGeometryToos.GetSolid(gElement, out result);
			return result;
		}

		public static int GetLineToSolid(Solid inSolid, Transform transform, Curve template, XYZ normal, double dAngle, ref List<Line> lstTarLine)
		{
			List<Line> list = new List<Line>();
			double num = Math.Cos(dAngle);
			XYZ xyz = template.GetEndPoint(1) - template.GetEndPoint(0);
			xyz = xyz.Normalize();
			foreach (object obj in inSolid.Edges)
			{
				Edge edge = (Edge)obj;
				if (Math.Abs(Math.Abs((edge.AsCurve().GetEndPoint(1) - edge.AsCurve().GetEndPoint(0)).Normalize().DotProduct(xyz)) - num) < 0.05 && edge.AsCurve() as Line != null)
				{
					list.Add(edge.AsCurve() as Line);
				}
			}
            //from k in list
            //where Math.Abs((k.GetEndPoint(1) - k.GetEndPoint(0)).Normalize().DotProduct(normal.Normalize())) < 0.05
            //select k;
			lstTarLine = list;
			return lstTarLine.Count;
		}

		public static XYZ CalculateFootPoint(XYZ ptStart, XYZ ptEnd, XYZ point)
		{
			if (ptStart.IsAlmostEqualTo(ptEnd))
			{
				return null;
			}
			double num = Math.Sqrt((ptEnd.X - ptStart.X) * (ptEnd.X - ptStart.X) + (ptEnd.Y - ptStart.Y) * (ptEnd.Y - ptStart.Y) + (ptEnd.Z - ptStart.Z) * (ptEnd.Z - ptStart.Z));
			num = ((ptEnd.X - ptStart.X) * (point.X - ptStart.X) + (ptEnd.Y - ptStart.Y) * (point.Y - ptStart.Y) + (ptEnd.Z - ptStart.Z) * (point.Z - ptStart.Z)) / (num * num);
			double num2 = ptStart.X + (ptEnd.X - ptStart.X) * num;
			double num3 = ptStart.Y + (ptEnd.Y - ptStart.Y) * num;
			double num4 = ptStart.Z + (ptEnd.Z - ptStart.Z) * num;
			return new XYZ(num2, num3, num4);
		}

		private static bool GetSolid(GeometryElement gElement, out Solid solid)
		{
			bool result = false;
			Solid solid2 = null;
			foreach (GeometryObject geometryObject in gElement)
			{
				solid2 = (geometryObject as Solid);
				if (solid2 != null || solid2.Edges.Size != 0)
				{
					result = true;
					break;
				}
			}
			solid = solid2;
			return result;
		}

		private static Line LiftAtBothEnds(Line inLine, double Length)
		{
			XYZ xyz = inLine.GetEndPoint(1) - inLine.GetEndPoint(0);
			double length = xyz.GetLength();
			double num = 202.0 * BreamConnectColumnGeometryToos.OneMillimeter / length;
			XYZ xyz2 = xyz + xyz * num + inLine.GetEndPoint(0);
			XYZ xyz3 = -(xyz * num) + inLine.GetEndPoint(0);
			if ((xyz3 - xyz2).GetLength() >= BreamConnectColumnGeometryToos.OneMillimeter)
			{
				return Line.CreateBound(xyz2, xyz3);
			}
			return null;
		}

		private static double dMILLIMETER_TO_FEET = 304.8;
	}
}
